package com.ks.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import com.ks.user.controller.dto.PowerResurceRequest;
import com.ks.user.dao.PowerRoleResurceMapper;
import com.ks.user.entity.OrgBase;
import com.ks.user.entity.PowerResurce;
import com.ks.user.dao.PowerResurceMapper;
import com.ks.user.entity.PowerRoleResurce;
import com.ks.user.entity.UserBase;
import com.ks.user.entity.vo.PowerResurceRelation;
import com.ks.user.service.IPowerResurceService;
import com.ks.user.service._ServiceBase;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限资源表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-11-30
 */
@Service
public class PowerResurceServiceImpl extends _ServiceBase<PowerResurceMapper, PowerResurce> implements IPowerResurceService {
    @Resource
    private PowerResurceMapper powerResurceMapper;

    @Resource
    private PowerRoleResurceMapper powerRoleResurceMapper;

    @Override
    public int addPowerResurce(OperationContext context, PowerResurce powerResurce) {
        Integer insert = powerResurceMapper.insert(powerResurce);
        return insert;
    }

    @Override
    public Map<String, Object> selectAppMenu(OperationContext context, int id) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<PowerResurce> wrapper = new EntityWrapper<>();
        PowerResurce powerResurce = new PowerResurce();
        powerResurce.setCApId(id);
        powerResurce.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(powerResurce);
        List<PowerResurce> powerResurces = powerResurceMapper.selectList(wrapper);
        map.put("list", powerResurces);
        return map;
    }

    @Override
    public int deleteAppMenu(OperationContext context, PowerResurce powerResurces) {
        Integer i = powerResurceMapper.deleteById(powerResurces);
        return i;
    }

    @Override
    public Map<String, Object> getRoleList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<PowerRoleResurce> wrapper = new EntityWrapper<>();
        PowerRoleResurce powerRoleResurce = new PowerRoleResurce();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iRoleID".equals(field.getName())) {
                    powerRoleResurce.setIRoleID(Integer.valueOf(field.getValue()));
                }
            }
        }
        powerRoleResurce.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(powerRoleResurce);
        Integer integer = powerRoleResurceMapper.selectCount(wrapper);
        map.put("count", integer);
        List<PowerRoleResurce> powerRoleResurces = powerRoleResurceMapper.selectPage(new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit()), wrapper);
        List<PowerResurce> list = new ArrayList<>();
        if (ToolUtil.isNotEmpty(powerRoleResurces)) {
            for (PowerRoleResurce roleResurce : powerRoleResurces) {
                PowerResurce powerResurce = powerResurceMapper.selectById(roleResurce.getIResurceID());
                list.add(powerResurce);
            }
        }
        map.put("list", list);
        return map;
    }

    @Override
    public int updateRower(OperationContext context, PowerResurce powerResurce) {
        Integer integer = powerResurceMapper.updateById(powerResurce);
        return integer;
    }

    @Override
    public Map<String, Object> addPowerRes(OperationContext context, PowerResurceRequest powerResurceRequest) {
        PowerResurce powerResurce = new PowerResurce();
        Map<String, Object> result = new HashMap<>();
        powerResurce.setIResourcesName(powerResurceRequest.getResourcesName());
        powerResurce.setCResourcesAttribute(powerResurceRequest.getResourcesAttribute());
        powerResurce.setCResourcesType(powerResurceRequest.getResourcesType());
        powerResurce.setCApId(powerResurceRequest.getApId());
        powerResurce.setCreateUserID(context.getUserId().intValue());
        if (isSystem(context)) {
            Integer i = powerResurceMapper.insert(powerResurce);
            if (i > 0) {
                result.put("data", powerResurce);
            } else {
                throw new GunsException("权限资源添加失败!");
            }
        } else {
            powerResurce.setOrgID(context.getOrgId().intValue());
            powerResurce.setOrgSubID(context.getOrgSubId().intValue());
            powerResurce.setTenantID(context.getTenantId().intValue());
            powerResurce.setDataType("TENANT");
            Integer i = powerResurceMapper.insert(powerResurce);
            if (i > 0) {
                result.put("data", powerResurce);
            } else {
                throw new GunsException("权限资源添加失败!");
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> updatePowerRes(OperationContext context, PowerResurceRequest powerResurceRequest) {
        PowerResurce powerResurce = new PowerResurce();
        Map<String, Object> result = new HashMap<>();
        powerResurce.setIResourcesName(powerResurceRequest.getResourcesName());
        powerResurce.setCResourcesAttribute(powerResurceRequest.getResourcesAttribute());
        powerResurce.setCResourcesType(powerResurceRequest.getResourcesType());
        powerResurce.setCApId(powerResurceRequest.getApId());
        powerResurce.setUpdateUserID(context.getUserId().intValue());
        powerResurce.setId(powerResurceRequest.getId());
        Integer i = powerResurceMapper.updateById(powerResurce);
        if (i > 0) {
            result.put("data", powerResurce);
        } else {
            throw new GunsException("权限资源修改失败!");
        }
        return result;
    }

    @Override
    public Map<String, Object> selectPowerRes(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        PowerResurce powerResurce = new PowerResurce();
        if (isSystem(context)) {
            //powerResurce.setDataType("SYS");
        } else {
            powerResurce.setOrgSubID(context.getOrgSubId().intValue());
            powerResurce.setTenantID(context.getTenantId().intValue());
            powerResurce.setOrgID(context.getOrgId().intValue());
            powerResurce.setCResourcesAttribute(2);
            powerResurce.setCResourcesType(3);
        }
        powerResurce.setDelMark(Const.DEL_FLAG_NORMAL);
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<PowerResurceRelation> powerRoleList = powerResurceMapper.PowerResurceRelationInfo(limit, powerResurce);
        Integer count = powerResurceMapper.PowerResurceRelationCount(powerResurce);
        Map<String, Object> resultMap = this.isResultMap(powerRoleList, searchCriteriaBase);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int deletePowerRes(OperationContext context, PowerResurceRequest powerResurceRequest) {
        EntityWrapper<PowerRoleResurce> wrapper = new EntityWrapper<>();
        PowerRoleResurce powerRoleResurce = new PowerRoleResurce();
        powerRoleResurce.setIResurceID(powerResurceRequest.getId());
        powerRoleResurce.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(powerRoleResurce);
        List<PowerRoleResurce> powerRoleResurces = powerRoleResurceMapper.selectList(wrapper);
        if(powerRoleResurces.size()>0){
            throw new GunsException("该权限资源已被引用无法删除!");
        }
        PowerResurce powerResurce = new PowerResurce();
        powerResurce.setId(powerResurceRequest.getId());
        if (!isSystem(context)) {
            EntityWrapper<PowerResurce> wr = new EntityWrapper<>();
            powerResurce.setDataType("SYS");
            wr.setEntity(powerResurce);
            List<PowerResurce> powerResurces = powerResurceMapper.selectList(wr);
            if(powerResurces.size()>0){
                throw new GunsException("系统权限资源无法删除!");
            }
        }
        powerResurce.setDelMark(Const.DEL_FLAG_DEL);
        Integer i = powerResurceMapper.updateById(powerResurce);
        return i;
    }


    public boolean isSystem(OperationContext context) {
        Long id = context.getTenantId();
        if (id == 0L || id == 0) {
            return true;
        }
        return false;
    }

    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }
}
